﻿using SAO.Models;
using SAO.OracleHelper.ReflectionExtensions;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace SAO.OracleHelper.ValiDateModel
{
    public class CenterContent
    {
        /// <summary>
        /// 验证实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static ResultMsg ValiDate<T>(T t)
        {
            ResultMsg resultMsg = new ResultMsg();
            Type type = typeof(T);
            foreach (PropertyInfo prop in type.GetProperties())
            {
                if (prop.IsDefined(typeof(ValidateAttribute), true))
                {
                    object value = prop.GetValue(t);
                    //拿到属性上所有alidateAttribute特性
                    IEnumerable<ValidateAttribute> validateAttributes = prop.GetCustomAttributes<ValidateAttribute>();
                    foreach (ValidateAttribute attribute in validateAttributes)
                    {
                        resultMsg = attribute.Validate(value);
                        //只要一个验证不通过就返回
                        if (!resultMsg.Sucess)
                        {
                            return resultMsg;
                        }
                    }
                }
            }
            return resultMsg;
        }
    }
}
